// pages/W_OrderList/index.js
var app = getApp()
var Keys = require('../../assets/data/keys.js')

Page({

  /**
   * 页面的初始数据
   */
	data: {
		TabTypeArray: ['已取消', '待付款', '待收货',  '已完成'],
		Butten1TextArray: ['', '取消', '查看物流', ''],
		Butten2TextArray: ['再次购买', '付款', '确认收货', '评价','已评论'],
		TabTypeIndex: 0,//当前订单分类的编号
		TabIndexDataList: [],//当前订单分类的列表数据
		ScrollTopView: 'scroll_top_view',//切换分类时将ScrollView滑动至顶层
    popHideen:false,
    select: "",
    Money: "",
    PayType: "",
    wMoney: "",//混合支付
    wMoneyHidden: false,//混合支付
    realPrice:"",
    O_ID:"",
    h:"",
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
    var _this = this;
    this.reqNetListData('待付款')
    this.setData({
      TabTypeIndex: "1",
      // ScrollTopView: this.data.ScrollTopView,

    })
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    
   

	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},


	/**
 * 切换订单分类
 */
	btnEventChangeTab: function (e) {
    // console.log(this.data.ScrollTopView);
		this.setData({
			TabTypeIndex: e.currentTarget.dataset.index,
			ScrollTopView: this.data.ScrollTopView,
		})

      this.reqNetListData(this.data.TabTypeArray[e.currentTarget.dataset.index]);
	},

	/**
	 * ScrollView下拉刷新
	 */
	bindEventScrollViewUpdate: function () {
		// this.reqNetListData('全部')
	},

	/**
	 * 请求列表数据
	 */
	reqNetListData: function (listType) {
    console.log("输出");
		wx.showNavigationBarLoading()
		var _this = this;
    console.log(listType)
		wx.request({
      url: "https://www.chnode.xyz/api/Order_State.ashx?",
			data: {
        DataType: "OrderState_List",
        Token: getApp().data.Token,
				O_State: listType,
				// page: 1,
			},
			success: function (res) {
        console.log("vvvvvv");
				console.log(res.data);
        console.log(getApp().data.Token);
        if (res.data.type == "datas") {
					_this.setData({
            TabIndexDataList: res.data.datas.OrderInfo,
					})
				}else{
          _this.setData({
            TabIndexDataList: "",
          })
        }




			},
			complete: function () { wx.hideNavigationBarLoading() }
		})
	},

	test: function (e) {
		console.log(e)
	},

  ClickedLeft:function(e){
    var _this = this;
    console.log(_this.data.TabTypeIndex);
    console.log("left");
    console.log(getApp().data.Token);
    // console.log(_this.data.TabTypeArray[_this.data.TabTypeIndex]);

    if (_this.data.TabTypeIndex == 2){
      console.log(e.currentTarget.dataset.oid)
      wx.navigateTo({
        url: '../Fl_Wuliu/index?id=' + e.currentTarget.dataset.oid, 
      })
    }else if (_this.data.TabTypeIndex == 1){
      console.log("取消订单");


      wx.showModal({
        title: '提示',
        content: '是否取消订单',

        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.request({
              url: 'https://www.chnode.xyz/api/Order_State.ashx?',
              data: {
                DataType: "OrderCancel",
                Token: getApp().data.Token,
                O_ID: e.currentTarget.dataset.oid,
                // page: 1,
              },
              success: function (res) {
                console.log(res);

                wx.showModal({
                  title: '提示',
                  content: '取消成功',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      _this.reqNetListData(_this.data.TabTypeArray[_this.data.TabTypeIndex]);
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })

              },
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })




    }
  },

  ClickedRight:function(e){
    var _this = this;
    console.log(e.currentTarget.dataset.oid);
    console.log(_this.data.TabTypeIndex);
    console.log("right");
    if (_this.data.TabTypeIndex == 0){
      console.log("再次购买");
      
      wx.switchTab({
        url: '../Fl_Index/index',
      })

    } else if (_this.data.TabTypeIndex == 1){

      console.log("fukuan");

      
      //判断价格
      wx.request({
        url: 'https://www.chnode.xyz/api/Balance.ashx?',
        data: {
          DataType: "MyBalance",
          Token: getApp().data.Token,
        },
        success: function (res) {
          console.log("vvvvvvvv");
          console.log(res);
          console.log(res.data.data.U_Blance);
          _this.setData({
            Money: res.data.data.U_Blance,
            realPrice: e.currentTarget.dataset.o_ymoney

          })
          console.log("bbbbbbb");
          console.log(e);
          realPrice: e.currentTarget.dataset.o_ymoney
          console.log(_this.data.realPrice);

         
          var tempMonet = parseFloat(_this.data.realPrice) - parseFloat(_this.data.Money)
          tempMonet = tempMonet.toFixed(2);
          _this.setData({
            wMoney: tempMonet,
          })


          if (parseFloat(_this.data.Money) > 0 && parseFloat(_this.data.wMoney) > 0) {
            _this.setData({
              wMoneyHidden: false,
            })
          } else {
            _this.setData({
              wMoneyHidden: true,
            })
          }

        }
      })

      // if (_this.data.Money == 0){
      //   _this.setData({
      //     h: "2000rpx",
      //   })
      // } 


      wx.showModal({
        title: '提示',
        content: '是否付款',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')

            _this.setData({
              select: "1",
              PayType: "0",
              popHideen:true,
              O_ID: e.currentTarget.dataset.oid,
            })




            /*
            wx.request({
              url: 'https://www.chnode.xyz/api/Pay.ashx?',
              data: {
                DataType: "DemoPay",
                O_ID: e.currentTarget.dataset.oid,
                // page: 1,
              },
              success: function (res) {
                console.log(res);

                if (res.data.isok == "true") {
                  wx.showModal({
                    title: '提示',
                    content: '付款成功',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                        _this.reqNetListData(_this.data.TabTypeArray[_this.data.TabTypeIndex]);
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                }



              },
            })
            */
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
        

      })

    } else if (_this.data.TabTypeIndex == 2) {
      console.log("vvvvvvbbbbbbb");
      var _this = this;
      console.log("确认收获");
      console.log(_this.data.TabIndexDataList);
      console.log(e.currentTarget.dataset.wuliu)


      if (e.currentTarget.dataset.wuliu.length == 0) {
        console.log("没物流");
        wx.showModal({
          title: '提示',
          content: '物流正在路上',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else {
        console.log("有物流");
        wx.showModal({
          title: '提示',
          content: '是否确认收货',

          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.request({
                url: 'https://www.chnode.xyz/api/Order_State.ashx?',
                data: {
                  DataType: "OrderEnter",
                  Token: getApp().data.Token,
                  O_ID: e.currentTarget.dataset.oid,
                  // page: 1,
                },
                success: function (res) {
                  console.log(res);

                  if (res.data.isok == "true") {
                    wx.showModal({
                      title: '提示',
                      content: '收货成功',
                      showCancel: false,
                      success: function (res) {
                        if (res.confirm) {
                          console.log('用户点击确定')
                          _this.reqNetListData(_this.data.TabTypeArray[_this.data.TabTypeIndex]);
                        } else if (res.cancel) {
                          console.log('用户点击取消')
                        }
                      }
                    })
                  }



                },
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

      





    } else if (_this.data.TabTypeIndex == 4) {
      console.log("确认收获");



      wx.showModal({
        title: '提示',
        content: '是否确认',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.request({
              url: 'https://www.chnode.xyz/api/Order_State.ashx?',
              data: {
                DataType: "OrderSetupEnter",
                Token: getApp().data.Token,
                O_ID: e.currentTarget.dataset.oid,
                // page: 1,
              },
              success: function (res) {
                console.log(res);

              if(res.data.isok == "true"){
                wx.showModal({
                  title: '提示',
                  content: '确认成功',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      _this.reqNetListData(_this.data.TabTypeArray[_this.data.TabTypeIndex]);
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }

                


              },
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })





    } else if (_this.data.TabTypeIndex == 3){
      var _this = this;
      console.log(e.currentTarget.dataset.oid);
      console.log(_this.data.TabIndexDataList);
      console.log(e.currentTarget.dataset.index);
      if (_this.data.TabIndexDataList[e.currentTarget.dataset.index].O_IsComment == 0){
        wx.navigateTo({
          url: '../Fl_Pinglun/index?O_ID=' + e.currentTarget.dataset.oid,
        })
      }else{

        wx.showModal({
          title: '提示',
          content: '您已经评论',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }

    }

  },

  itemClicked:function(e){
    var _this = this;
    console.log(e.currentTarget.dataset.index);
    console.log("点击");
    console.log(_this.data.TabIndexDataList[e.currentTarget.dataset.index].O_ID);
    wx.navigateTo({
      url: '../Fl_DingdanDetail1/index?O_ID=' + _this.data.TabIndexDataList[e.currentTarget.dataset.index].O_ID,
    })
  },

  popHidden:function(){
  var _this = this;
  if (_this.data.popHideen == true){
    _this.setData({
      popHideen : false,
    })
  }else{
    _this.setData({
      popHideen : true,
    })
  }
  },

  //余额支付
  one: function () {
    console.log("余额支付");
    var _this = this;
    _this.setData({
      select: "2",
      PayType: "2",
    })
  },

  //微信+余额支付
  two: function () {
    console.log("微信+余额支付")
    var _this = this;
    _this.setData({
      select: "3",
      PayType: "1",
    })
  },

  //微信支付
  three: function () {
    console.log("微信支付")
    var _this = this;
    _this.setData({
      select: "1",
      PayType: "0",
    })
  },

  quedingClicked:function(){

    //支付功能
    var _this = this;
    console.log(_this.data.O_ID);


    if (_this.data.PayType == "0") {
      console.log("微信支付");
      wx.request({
        url: 'https://www.chnode.xyz/api/Pay.ashx?',
        data: {
          DataType: "Wx_Pay",
          PayType: _this.data.PayType,
          O_ID: _this.data.O_ID,
          Token: getApp().data.Token
        },
        success: function (res) {
          console.log("aaaaaaa");
          console.log(res);
          console.log("aaaaaaa");

          if (res.data.isok == "false") {
            console.log("请求失败")
            wx.showModal({
              title: '提示',
              content: res.data.info,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            console.log("请求成功");
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              "package": res.data.data.package,
              signType: 'MD5',
              paySign: res.data.data.paySign,
              fail: function (aaa) {
                console.log(aaa);
                wx.showToast({ title: '支付失败:' + aaa })
                // wx.showModal({
                //   title: '提示',
                //   content: "订单已经生成",
                //   showCancel: false,
                //   success: function (res) {
                //     if (res.confirm) {
                //       console.log('用户点击确定')
                //       wx.redirectTo({
                //         url: '../W_OrderList/index'
                //       });
                //     } else if (res.cancel) {
                //       console.log('用户点击取消')
                //     }
                //   }
                // })

              },
              success: function () {
                wx.showToast({ title: '支付成功' })
                wx.redirectTo({
                  url: '../W_OrderList/index'
                });
              }
            })
          }

        }
      })
    } else if (_this.data.PayType == "1") {
      console.log("微信加于额支付")


      wx.request({
        url: 'https://www.chnode.xyz/api/Pay.ashx?',
        data: {
          DataType: "Wx_Pay",
          PayType: _this.data.PayType,
          O_ID: _this.data.O_ID,
          Token: getApp().data.Token
        },
        success: function (res) {
          console.log("aaaaaaa");
          console.log(res);
          console.log("aaaaaaa");

          if (res.data.isok == "false") {
            console.log("请求失败")
            wx.showModal({
              title: '提示',
              content: res.data.info,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {

            console.log("请求成功");
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              "package": res.data.data.package,
              signType: 'MD5',
              paySign: res.data.data.paySign,
              fail: function (aaa) {
                wx.showToast({ title: '支付失败:' + aaa })
                // wx.showModal({
                //   title: '提示',
                //   content: "订单已经生成",
                //   showCancel: false,
                //   success: function (res) {
                //     if (res.confirm) {
                //       console.log('用户点击确定')
                //       wx.redirectTo({
                //         url: '../W_OrderList/index'
                //       });
                //     } else if (res.cancel) {
                //       console.log('用户点击取消')
                //     }
                //   }
                // })

              },
              success: function () {
                wx.showToast({ title: '支付成功' })
                wx.redirectTo({
                  url: '../W_OrderList/index'
                });
              }
            })
          }


        }
      })
    } else if (_this.data.PayType == "2") {
      wx.request({
        url: 'https://www.chnode.xyz/api/Pay.ashx?',
        data: {
          DataType: "Wx_Pay",
          PayType: _this.data.PayType,
          O_ID: _this.data.O_ID,
          Token: getApp().data.Token
        },
        success: function (res) {
          console.log(res);

          wx.showModal({
            title: '提示',
            content: res.data.info,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack({
                  delta: 1
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })


        }
      })
    }


  }
})